<?php
//+-----------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+-----------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+-----------------------------------------------------------
//| 
//+-----------------------------------------------------------


namespace app\common\validate;


use app\common\exception\BaseException;
use think\captcha\facade\Captcha;
use think\exception\ValidateException;

class BaseValidate extends \think\Validate
{
    public function go($data,$scene=''){
        try {
        	if(array_key_exists($scene, $this->scene)){
		        $this->failException(true)->scene($scene)->check($data);
	        }else{
		        $this->failException(true)->check($data);
	        }
        }catch(ValidateException $e){
            return $this->showException($e);
        }
    }

    /**
     * 其他子类接管异常
     * @param ValidateException $exception
     */
    protected function showException(ValidateException $exception){
        throw new BaseException($exception->getError(),$exception->getCode());
    }

	/**
	 * 密码复杂度检查
	 * @param $value
	 * @return bool
	 */
    protected function checkPasswd($value,$rule,$data){
    	if(isset($data['account']) && $data['account'] == 'admin'){
    		return true;
	    }
    	if(!preg_match('/(?=.*[A-Z])/',$value)){
			throw new ValidateException('密码至少包含一位大写字母');
	    }
    	
    	if(!preg_match('/(?=.*[a-z])/',$value)){
		    throw new ValidateException('密码至少包含一位小写字母');
	    }

	    if(!preg_match('/(?=.*[\d])/',$value)){
		    throw new ValidateException('密码至少包含一位数字');
	    }

	    return true;
    }

	/**
	 * 检查验证码
	 * @param $value
	 * @return bool
	 */
    protected function checkCaptcha($value){
	    if( !captcha_check($value))
	    {
		  throw new ValidateException('验证码错误');
	    }
	    return true;
    }
}